﻿using EventBus;
using LiteStep.Currencies;
using LiteStep.Events;
using LiteStep.Exchanges.MtGox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteStep.Utils.Recorders.MtGox
{
	public class MtGoxTickerRecorder<TFrom, TTo> : ITickerRecorder<Ticker<TFrom, TTo>>
		where TFrom : ICurrency<TFrom>, new()
		where TTo : ICurrency<TTo>, new()
	{
		protected IEventBus eventBus { get; set; }
		private IHandlerRegistration registration { get; set; }

		public MtGoxTickerRecorder(IEventBus eventBus)
		{
			this.eventBus = eventBus;
		}

		public void Register()
		{
			if (registration == null)
			{
				registration = eventBus.AddHandler(AfterTickerResponseEvent<Ticker<TFrom, TTo>>.TYPE, this);
			}
		}

		public void Unregister()
		{
			if (registration != null)
			{
				registration.RemoveHandler();
				registration = null;
			}
		}

		public void OnAfterTickerResponseEvent(AfterTickerResponseEvent<Ticker<TFrom, TTo>> e)
		{
			if (e == null || e.Ticker == null)
			{
				LiteStep.Log.ErrorFormat("The MtGox {0}/{1} ticker was not received", typeof(TFrom).Name.ToUpper(), typeof(TTo).Name.ToUpper());
				return; 
			}

			try
			{
				using (var liteStepContext = LiteStep.Ninjector.Get<LiteStepContext>())
				{
					var ticker = new MtGoxTicker();
					ticker.Base = (int)Enum.Parse(typeof(Currencies.Currencies), typeof(TFrom).Name);
					ticker.Alt = (int)Enum.Parse(typeof(Currencies.Currencies), typeof(TTo).Name);
                    ticker.High = e.Ticker.High.ValueInt * ValueIntMultiplier.Get(e.Ticker.High.Currency);
                    ticker.Low = e.Ticker.Low.ValueInt * ValueIntMultiplier.Get(e.Ticker.Low.Currency);
                    ticker.Average = e.Ticker.Average.ValueInt * ValueIntMultiplier.Get(e.Ticker.Average.Currency);
                    ticker.VWAP = e.Ticker.VWAP.ValueInt * ValueIntMultiplier.Get(e.Ticker.VWAP.Currency);
                    ticker.Volume = e.Ticker.Volume.ValueInt * ValueIntMultiplier.Get(e.Ticker.Volume.Currency);
                    ticker.LastLocal = e.Ticker.LastLocal.ValueInt * ValueIntMultiplier.Get(e.Ticker.LastLocal.Currency);
                    ticker.LastOrig = e.Ticker.LastOrig.ValueInt * ValueIntMultiplier.Get(e.Ticker.LastOrig.Currency);
                    ticker.LastAll = e.Ticker.LastAll.ValueInt * ValueIntMultiplier.Get(e.Ticker.LastAll.Currency);
                    ticker.Last = e.Ticker.Last.ValueInt * ValueIntMultiplier.Get(e.Ticker.Last.Currency);
                    ticker.Buy = e.Ticker.Buy.ValueInt * ValueIntMultiplier.Get(e.Ticker.Buy.Currency);
                    ticker.Sell = e.Ticker.Sell.ValueInt * ValueIntMultiplier.Get(e.Ticker.Sell.Currency);
					ticker.Version = 1;
                    ticker.Timestamp = e.Ticker.Timestamp;
					ticker.InsertedOn = DateTime.Now;

					liteStepContext.MtGoxTickers.Add(ticker);
					liteStepContext.SaveChanges();
				}
			}
			catch (Exception ex)
			{
				LiteStep.Log.Error(String.Format("There was an error saving the MtGoc {0}/{1} ticker", typeof(TFrom).Name.ToUpper(), typeof(TTo).Name.ToUpper()), ex);
			}
			
		}
	}
}
